package com.erp.module.zhichengJianyan.XXInspection

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.ProcessInspectionJieHaoBean
import com.erp.bean.ZhiLiangBean
import com.erp.module.zhiLiang.xingJianZhiLiang.XjZhiLiangKView
import com.erp.module.zhichengJianyan.ProcessInspectionContract
import com.erp.module.zhichengJianyan.ProcessInspectionModel
import com.erp.util.JsonUtil
import org.json.JSONException
import org.json.JSONObject

class IXxInspection(val V: ProcessInspectionContract.XxInpectionView, val context: Context, var probean: ProcessInspectionJieHaoBean): ProcessInspectionContract.IXxInpection{
    var M = ProcessInspectionModel()
    override fun getInpectionBean(id: String) {
        M.getInspection(context,id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                Log.d("getInpectionBean",data)
                if(data == null){
                    V.getInpectionNo()
                    return
                }
                jsondata(data)
                V.getInpectionOk()
            }

            override fun onNot(throwable: String?) {
                Log.d("getInpectionBean",throwable)
                V.getInpectionNo()
            }

        })
    }

    override fun deleteInpection(id: String) {
        M.deleteInspection(context,id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                Log.d("addInpection",data)
                V.deleteInpectionOk()
            }

            override fun onNot(throwable: String?) {
                Log.d("addInpection",throwable.toString())
                V.deleteInpectionNo()
            }

        })
    }

    override fun addInpection(bean: ProcessInspectionJieHaoBean, deletephoto: MutableList<ProcessInspectionJieHaoBean.photo>) {
        M.addInspection(context,bean,deletephoto,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data == null){
                    V.addInpectionNo()
                    return
                }
                Log.d("TAAAG",data)
                jsondata(data)
                V.addInpectionOk()
            }

            override fun onNot(throwable: String?) {
                Log.d("addInpection",throwable.toString())
                V.addInpectionNo()
            }

        })
    }

    fun jsondata(data : String){
        try {
            val jo = JSONObject(data)
            probean.inspection_id = JsonUtil.getvolue(jo, "inspection_id")
            probean.batch = JsonUtil.getvolue(jo, "batch")
            probean.plan_warehousing_time = JsonUtil.getvolue(jo, "plan_warehousing_time")
            probean.process_inspection_time = JsonUtil.getvolue(jo, "process_inspection_time")
            probean.process_inspection_name = JsonUtil.getvolue(jo, "process_inspection_name")
            probean.ctime = JsonUtil.getvolue(jo, "ctime")
            val jsp = JsonUtil.getjsvolue(jo, "files")
            probean.listphoto.clear()
            if (jsp != null) {
                for (j in 0 until jsp.length()) {
                    val jop = jsp.get(j) as JSONObject
                    val photo = ProcessInspectionJieHaoBean.photo()
                    photo.fid = JsonUtil.getvolue(jop, "fid")
                    photo.filename = JsonUtil.getvolue(jop, "filename")
                    photo.uri = JsonUtil.getvolue(jop, "uri")
                    probean.listphoto.add(photo)
                }
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

}